﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrNhapGoSay : DevComponents.DotNetBar.Office2007Form
    {
        public FrNhapGoSay()
        {
            InitializeComponent();
        }
        private string ChungTu1,ChungTuKGS,LoaiGo1, child = " - ", LoSay1="", TinhTrangGo1="",LyDoNhap1="",AdorKD="";
        //private string sql_select = @"select * from dbo.Serial_NKGo";
        public void User(string User)
        {
            lb_User.Text = User;
        }
        private void combo_load()
        {
            Process dta = new Process();
            FormProcess Frp = new FormProcess();
            string[] arr = new string[] { "AD", "KD" };
            for (int i = 0; i < arr.Length; i++)
            {
                cb_AdorKD.Items.Add(arr[i].ToString());
                //cb_PhanLoai.Items.Add(arr[i].ToString());
                //.Items.Add(arr[i].ToString());
            }
            string[] arr_TinhTrang = new string[] { "Tròn", "Vuông","Phách","Sơ Chế","Kiện"};
            for (int j = 0; j < arr_TinhTrang.Length; j++)
            {
                cb_TinhTrangGo.Items.Add(arr_TinhTrang[j].ToString());
            }
            string sql_LoaiGo = @"select MaLoai from dbo.LoaiGo";
            SqlConnection Conn = dta.Connect();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql_LoaiGo;
            cmd.Connection = Conn;
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                cb_LoaiGo.Items.Add(rd["MaLoai"].ToString());
                //.Items.Add(rd[column1].ToString());      
            }
            sql_LoaiGo = @"select MaLyDo,LyDo from dbo.LyDo where MaLyDo like 'N%'";
            Frp.cb1(cb_LyDoNhap, sql_LoaiGo, 1, 0);
            Conn.Close();
            cmd.Dispose();
            rd.Close();

        }

        private void FrNhapGoSay_Load(object sender, EventArgs e)
        {
            combo_load();
        }
        private void Save()
        {
            Process prs = new Process();
            LoaiGo1 = cb_LoaiGo.Text.Trim();
            LoSay1 = txt_LoSay.Text.Trim();
            LyDoNhap1 = prs.substring_right(cb_LyDoNhap.Text, child);
            LyDoNhap1 = cb_LyDoNhap.Text.Trim();
            ChungTuKGS = txt_ChungTu.Text;
            try
            {
                if (cb_TinhTrangGo.Text == "Tròn" || cb_TinhTrangGo.Text == "Vuông" || cb_TinhTrangGo.Text == "Sơ Chế" || cb_TinhTrangGo.Text == "Phách" || cb_TinhTrangGo.Text == "Kiện" || txt_ChungTu.Text == "")
                //if (AdorKD== "" || TinhTrangGo1 == "" || cb_LoaiGo.Text == ""||txt_ChungTu.Text==""
                {
                    AdorKD = cb_AdorKD.Text;
                    TinhTrangGo1 = cb_TinhTrangGo.Text;
                    string sql_insert = null;
                    SqlConnection Conn = prs.Connect();
                    Conn.Open();
                    string getmonth = @"select month(getdate())-month(NgayNhap)+year(getdate())-year(NgayNhap) from dbo.Serial_NKGo where Serial_NK=(select max(Serial_NK) from dbo.Serial_NKGo)";
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = getmonth;
                    cmd.Connection = Conn;
                    int month = int.Parse(cmd.ExecuteScalar().ToString());
                    string date = string.Format("{0:MM/yy}", DateTime.Now);
                    if (month > 0)
                    {
                        ChungTu1 = "1" + child + date + "/NKGS";


                    }
                    if (month <= 0)
                    {
                        string sql_ChungTu = @"select ChungTu From dbo.Serial_NKGo where Serial_NK=(select max(Serial_NK) from dbo.Serial_NKGo)";
                        cmd.CommandText = sql_ChungTu;
                        int ChungTu_input = int.Parse(prs.substring_left(cmd.ExecuteScalar().ToString(), child));
                        ChungTu1 = (ChungTu_input + 1).ToString() + child + date + "/NKGS";
                    }
                    sql_insert = @"insert into dbo.Serial_NKGo(ChungTu,ChungTuKGS,LoaiGo,LoSay,TinhTrangGo,LyDoNhap,ADorKD,NhapTu,NgayVaoLo,NgayRaLo) values(N'";
                    sql_insert += ChungTu1 + "',N'" + ChungTuKGS + "',N'" + LoaiGo1 + "',N'" + LoSay1 + "',N'" + TinhTrangGo1 + "',N'" + LyDoNhap1 + "',N'" + AdorKD + "',N'LSSCP','";
                    sql_insert += dateTimePicker2.Value + "','" + dateTimePicker1.Value + "')";

                    cmd.CommandText = sql_insert;
                    cmd.ExecuteNonQuery();
                    string sql_Serial = @"select Serial_NK from dbo.Serial_NKGo where Serial_NK=(select max(Serial_NK) from dbo.Serial_NKGo)";
                    cmd.CommandText = sql_Serial;
                    lb_Serial.Text = cmd.ExecuteScalar().ToString();
                    MessageBox.Show("Dữ liệu đã được thêm thành công");
                    prs.reset_txt(groupBox1);
                    bt_save.Enabled = false;
                    Conn.Close();
                    cmd.Dispose();
                    FrNhapChiTiet FrNCT = new FrNhapChiTiet();
                    FrNCT.Show();
                    FrNCT.User(lb_User.Text);
                    FrNCT.ChungTu(ChungTu1);
                    FrNCT.Get_Serial(lb_Serial.Text);
                    linkLabel1.Visible = true;

                }
                else
                {
                    MessageBox.Show("Kiểm tra lại thông tin nhập");
                    cb_TinhTrangGo.Focus();
                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { 
                    MessageBox.Show("Nhập sai thông tin trong Combobox");
                    cb_LoaiGo.Focus();
                }
                else
                    MessageBox.Show("Đã xảy ra lỗi khi thêm dữ liệu, Vui Lòng kiểm tra lại" + ex.Message);
            }
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void bt_insert_Click(object sender, EventArgs e)
        {
            Data dta = new Data();
            txt_ChungTu.ReadOnly = false;
            dta.reset_txt(groupBox1);
            //dta.reset_cb(groupBox1);
            //txt_ChungTu.Focus();
            bt_save.Enabled = true;
        }

        private void linkLabel1_Click(object sender, EventArgs e)
        {
            try
            {
                bt_save.Enabled = false;
                Data dta = new Data();
                txt_ChungTu.ReadOnly = true;
                int i = int.Parse(lb_Serial.Text);
                string sql_select = @"select * from dbo.Serial_NKGo where Serial_NK=" + i;
                SqlConnection Conn = dta.Connect();
                Conn.Open();
                SqlCommand cmd = new SqlCommand(sql_select, Conn);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    txt_ChungTu.Text = rd["ChungTu"].ToString();
                    dateTimePicker2.Value = DateTime.Parse(rd["NgayVaoLo"].ToString());
                    cb_LoaiGo.Text = rd["LoaiGo"].ToString();
                    cb_AdorKD.Text = rd["ADorKD"].ToString();
                    cb_LyDoNhap.Text = rd["LyDoNhap"].ToString();
                    txt_LoSay.Text = rd["LoSay"].ToString();
                    cb_TinhTrangGo.Text = rd["TinhTrangGo"].ToString();
                    dateTimePicker1.Value = DateTime.Parse(rd["NgayRaLo"].ToString());
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_edit_Click(object sender, EventArgs e)
        {

            if (txt_ChungTu.Text != "")
            {
                Process prs = new Process();
                FormProcess Frp = new FormProcess();
                DialogResult d = MessageBox.Show("Bạn có chắc muốn sửa Thông Tin của Chứng Từ " + txt_ChungTu.Text, "Thông Báo", MessageBoxButtons.OKCancel);
                if (d == DialogResult.OK)
                {
                    try
                    {
                        if (cb_AdorKD.Text == "AD" || cb_AdorKD.Text == "KD")
                        {
                            SqlConnection Conn = prs.Connect();
                            Conn.Open();
                            string sql_update = null;
                            ChungTu1 = txt_ChungTu.Text.Trim();
                            LoaiGo1 = cb_LoaiGo.Text.Trim();
                            AdorKD = cb_AdorKD.Text.Trim();
                            LyDoNhap1 = prs.substring_right(cb_LyDoNhap.Text, child);
                            if (LyDoNhap1 == "")
                            {
                                LyDoNhap1 = cb_LyDoNhap.Text;
                            }
                            LoSay1=txt_LoSay.Text.Trim();
                            int i = int.Parse(lb_Serial.Text);
                            sql_update = "update dbo.Serial_NKGo set LoaiGo=N'" + LoaiGo1 + "',LyDoNhap=N'";
                            sql_update += LyDoNhap1 +"',TinhTrangGo=N'"+cb_TinhTrangGo.Text+"',ADorKD=N'" + AdorKD + "',NgayVaoLo='";
                            sql_update += dateTimePicker2.Value+ "',LoSay=N'"+LoSay1+"',NgayRaLo='"+dateTimePicker1.Value+"' where Serial_NK=" + i;
                            //MessageBox.Show(sql_update);
                            SqlCommand cmd = new SqlCommand(sql_update, Conn);
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("Thông tin chỉnh sửa thành công");
                            prs.reset_txt(groupBox1);
                            Conn.Close();
                            cmd.Dispose();

                            cb_AdorKD.Text = "";
                            cb_LyDoNhap.Text = "";
                            cb_LoaiGo.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("Kiểm tra lại thông tin nhập");
                            cb_AdorKD.Focus();

                        }
                    }
                    catch
                    {
                        MessageBox.Show("Đã có lỗi xảy ra khi chỉnh sửa dữ liệu, Vui lòng kiểm tra lại");
                    }
                    bt_save.Enabled = false;
                }
            }
        }

        private void FrNhapGoSay_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyCode == Keys.F1)
                bt_insert_Click(null, null);
            if (ex.KeyCode == Keys.F11)
                Save();
        }
        private void Report_NKXK()
        {
            Data dta = new Data();
            int i = int.Parse(lb_Serial.Text);
            string sql_select = @"select Seri.ChungTu,Seri.NgayVaoLo as NgayNhan,Seri.ChungTuKGS,Seri.LoaiGo,Seri.LoSay,Seri.TinhTrangGo,Seri.NgayRaLo,Nhap.NguonCungCap,Seri.ADorKD,Seri.LoaiGo,Seri.NhapTu,Nhap.ChieuDay as ChieuRong,Nhap.ChieuRong as ChieuDai,Nhap.ChieuDai as ChieuCao,Nhap.SoKhoi,Nhap.SoLuong,Nhap.GhiChu
                                    from dbo.Serial_NKGo as Seri inner join dbo.NhapKhoGo as Nhap on Seri.Serial_NK=Nhap.Serial_NK
								    where Seri.Serial_NK=" + i;
            DataSet ds = dta.dts(sql_select);
            Fr_Report FrR = new Fr_Report();
            FrR.report_NKLS(ds);
            FrR.Show();
            ds.Dispose();
        }

        private void bt_report_Click(object sender, EventArgs e)
        {
            Report_NKXK();
        }



    }
}
